package com.nmgs.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.trkf.PasswordEncryption.PassWordUtils;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

public class PubTools {
   static SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
   static SimpleDateFormat dfYMD=new SimpleDateFormat("yyyy-MM-dd");

    public static final Map<String, String> CONFIG;

    static {
        Map<String, String> tempMap = new HashMap<>();
        tempMap.put("vehicleid", "车牌号");
        tempMap.put("invehicleid", "入口车牌");
        tempMap.put("obuvehicleid", "识别车牌");
        tempMap.put("vehaxiscount", "轴数");
        tempMap.put("vehfeeweight", "计重折后重量");
        tempMap.put("invehtype", "入口车型");
        tempMap.put("vehtype", "车型");
        tempMap.put("vehlimweight", "限载重量");
        tempMap.put("vehinweight", "实际重量");
        tempMap.put("invehweight", "入口计重");
        tempMap.put("selectcode", "查验结果");
        tempMap.put("select", "查验结果");
        tempMap.put("commandcode", "结果");
        tempMap.put("command", "查验结果");
        tempMap.put("invehaxiscount", "入口轴数");
        tempMap.put("enstationhex", "入口站名称");
        tempMap.put("enstationname", "入口站名称");
        tempMap.put("autovehtype", "识别车型");
        tempMap.put("autoaxiscount", "识别轴数");
        CONFIG = Collections.unmodifiableMap(tempMap);
    }
    public static final List<Map<String, String>> FLAG_LIST=null;

    /**
     * 判断字符串是否为空
     * yyp 20190305
     * @param anObject 字符串
     * @return true :是空，false:不是空
     */
    public static boolean isNull(String anObject ) {
        if(null==anObject||"".equals(anObject)||"null".equals(anObject)){
            return true;
        }else{
            return false;
        }
    }

    public static boolean isEmpty(Object anObject ) {
        if (anObject == null) return true;
        else if (anObject instanceof CharSequence) return ((CharSequence) anObject).length() == 0;
        else if (anObject instanceof Collection) return ((Collection) anObject).isEmpty();
        else if (anObject instanceof Map) return ((Map) anObject).isEmpty();
        else if (anObject.getClass().isArray()) return Array.getLength(anObject) == 0;

        return false;
    }

    /**
     *	根据⾝份证号获取年龄
     *
     * @param IDCard
     * @return
     */
    public static Integer getAge(String IDCard) {
        String age ="0";
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH)+1;
        int dayNow = cal.get(Calendar.DATE);
        String IDCardNew=((IDCard.length()==15)?IDCard.substring(0, 6)+"19"+IDCard.substring(6)+"0":IDCard);
        int year = Integer.valueOf(IDCardNew.substring(6,10));
        int month = Integer.valueOf(IDCard.substring(10,12));
        int day = Integer.valueOf(IDCard.substring(12,14));
        if ((month < monthNow) || (month == monthNow && day<= dayNow) ){
            age = String.valueOf(yearNow - year);
        }else {
            age = String.valueOf(yearNow - year-1);
        }
        return Integer.parseInt(age);
    }

    public static JSONArray revertList(JSONObject json){
        JSONArray listArray=new JSONArray();
        listArray.add(json);
        return listArray;
    }

    public static String getCurrentDate(){
        return df.format(new Date());
    }
    public static String getdfYMD(){
        return dfYMD.format(new Date());
    }
    public static String getDBType(){
        return PropertiesUtil.getValue("DBType");
    }
    public static String getStaNo(){
        return PropertiesUtil.getValue("StaNo");
    }
    public static String getUserType(){
        return PropertiesUtil.getValue("UserType");
    }



    public static String resolveContents(String str){
        JSONObject jo = new JSONObject();
        try{
          /*  ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readTree( String.format("\"%s\"", a.getContent()));*/
            Object strObj3=  JSONObject.parse(str);
            JSONObject strObj=  JSONObject.parseObject(strObj3.toString());
            for (String key : strObj.keySet()) {
                  Object value = strObj.get(key);
                System.out.println(key+":"+CONFIG.get(key.toLowerCase()));
                  jo.put(CONFIG.get(key.toLowerCase()),value);
              }
         /* }*/
        }catch (Exception e){
            e.printStackTrace();
            jo= new JSONObject();
        }
        return jo.toJSONString();
    }
    //获取IP连通性
    public static boolean pingIp(String ip,int timeoutSeconds){
        try {
            String cmd = System.getProperty("os.name").toLowerCase().contains("win")
                    ? "ping -n 1 -w " + (timeoutSeconds * 1000) + " " + ip
                    : "ping -c 1 -w " + timeoutSeconds + " " + ip;

            Process process = Runtime.getRuntime().exec(cmd);
            return process.waitFor() == 0;
        } catch (Exception e) {
            return false;
        }
    }
//获取当前日期所在一周的时间，从周一开始到周日
    public static Map<String,String> getCurrentWeekRange() {
        Map<String,String> map=new HashMap<>();
        LocalDate now = LocalDate.now();
        LocalDateTime localDateTime = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
                .atStartOfDay();
        LocalDateTime localDateTime1 = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).atTime(23, 59, 59);
        map.put("startTime",localDateTime.toString().replaceAll("T"," "));
        map.put("endTime",localDateTime1.toString().replaceAll("T"," "));
        return map;
    }

    public static String getDecNumDate(int b){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -b); // 将日期向前推b天
        Date date = calendar.getTime();
        return df.format(date).substring(0,10);
    }

    public static String getDateMonthDay(Date datePatam){
        Calendar cal = Calendar.getInstance();
        cal.setTime(datePatam);
        int month = cal.get(Calendar.MONTH) + 1;  // 月份从0开始需要+1
        int day = cal.get(Calendar.DAY_OF_MONTH);
        return month+"."+day;
    }
    public static void main(String[] args) {
        System.out.println(PubTools.getDecNumDate(0));
    }

    /**
     * double  double保留小数
     * @param number
     * @return
     */
    public static double doubleSave1(double number, int scale){
        BigDecimal bd = BigDecimal.valueOf(number);
        bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);
        double roundedNumber = bd.doubleValue();
        return roundedNumber;
    }
}


